package com.zondy.mapgis.psmap.manager.service.impl;

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zondy.mapgis.common.core.exception.web.BadRequestException;
import com.zondy.mapgis.common.core.utils.JsonUtils;
import com.zondy.mapgis.common.core.utils.StringUtils;
import com.zondy.mapgis.common.core.web.page.TableDataInfo;
import com.zondy.mapgis.common.security.utils.SecurityUtils;
import com.zondy.mapgis.domain.psmap.AppMapSheet;
import com.zondy.mapgis.psmap.manager.mapper.AppMapSheetMapper;
import com.zondy.mapgis.psmap.manager.service.IAppMapSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AppMapSheetServiceImpl extends ServiceImpl<AppMapSheetMapper, AppMapSheet> implements IAppMapSheetService {
   @Autowired
   private AppMapSheetMapper appMapSheetMapper;

   @Override
   public AppMapSheet selectAppMapSheetByMapSheetId(Long mapSheetId) {
      return this.appMapSheetMapper.selectAppMapSheetByMapSheetId(mapSheetId);
   }

   @Override
   public List<AppMapSheet> selectAppMapSheetList(AppMapSheet appMapSheet) {
      return this.appMapSheetMapper.selectAppMapSheetList(appMapSheet);
   }

   @Override
   public List<String> selectAppMapSheetScaleList(String rect, String originSrs, String destSrs) {
      AppMapSheet appMapSheet = new AppMapSheet();
      appMapSheet.setRect(rect);
      appMapSheet.setOriginSrs(originSrs);
      appMapSheet.setDestSrs(destSrs);
      List<AppMapSheet> list = this.appMapSheetMapper.selectAppMapSheetList(appMapSheet);
      return list.stream().map(AppMapSheet::getScale).collect(Collectors.toList());
   }

   @Override
   public int insertAppMapSheet(AppMapSheet appMapSheet) {
      return this.appMapSheetMapper.insertAppMapSheet(appMapSheet);
   }

   @Override
   public int updateAppMapSheet(AppMapSheet appMapSheet) {
      return this.appMapSheetMapper.updateAppMapSheet(appMapSheet);
   }

   @Override
   public int deleteAppMapSheetByMapSheetIds(Long[] mapSheetIds) {
      return this.appMapSheetMapper.deleteAppMapSheetByMapSheetIds(mapSheetIds);
   }

   @Override
   public int deleteAppMapSheetByMapSheetId(Long mapSheetId) {
      return this.appMapSheetMapper.deleteAppMapSheetByMapSheetId(mapSheetId);
   }

   @Override
   public TableDataInfo queryNos(String scale, String rect, String originSrs, String destSrs, String gdbp, String url, String keyword, int pageNum, int pageSize, Boolean replace) {
      AppMapSheet appMapSheetCondition = new AppMapSheet();
      appMapSheetCondition.setScale(scale);
      appMapSheetCondition.setRect(rect);
      appMapSheetCondition.setOriginSrs(originSrs);
      appMapSheetCondition.setDestSrs(destSrs);
      List<AppMapSheet> appMapSheetsList = this.appMapSheetMapper.selectAppMapSheetList(appMapSheetCondition);
      if (replace) {
         if (StringUtils.isNotEmpty(appMapSheetsList)) {
            this.appMapSheetMapper.deleteAppMapSheetByMapSheetId(appMapSheetsList.get(0).getMapSheetId());
         }
      } else if (StringUtils.isNotEmpty(appMapSheetsList)) {
         return this.pagingNos(appMapSheetsList.get(0).getNo(), keyword, pageNum, pageSize);
      }

      Map<String, String> obj = new HashMap<>();
      obj.put("f", "json");
      obj.put("scale", scale);
      obj.put("rect", rect);
      obj.put("srsId", originSrs);
      obj.put("objSrsId", destSrs);
      obj.put("sjGdbp", gdbp);
      StringBuilder params = new StringBuilder();
      obj.forEach((k, v) -> {
         params.append("&").append(k).append("=").append(v);
      });

      String resStr;
      try {
         resStr = HttpUtil.get(url + "?" + HttpUtil.encodeParams(params.substring(1), Charset.defaultCharset()));
      } catch (Exception var18) {
         throw new BadRequestException("从" + url + "获取资源失败");
      }

      try {
         JsonUtils.parseObject(resStr, List.class);
      } catch (Exception var17) {
         throw new BadRequestException("解析资源失败，资源内容为：" + resStr);
      }

      AppMapSheet newAppMapSheet = new AppMapSheet();
      newAppMapSheet.setScale(scale);
      newAppMapSheet.setRect(rect);
      newAppMapSheet.setOriginSrs(originSrs);
      newAppMapSheet.setDestSrs(destSrs);
      newAppMapSheet.setNo(resStr);
      newAppMapSheet.setCreateBy(SecurityUtils.getUsername());
      this.appMapSheetMapper.insertAppMapSheet(newAppMapSheet);
      return this.pagingNos(resStr, keyword, pageNum, pageSize);
   }

   private StringBuilder toQueryParam(Map<String, String> map) {
      StringBuilder param = new StringBuilder();
      map.forEach((k, v) -> {
         param.append(k).append("=").append(v).append("&");
      });
      return param;
   }

   private TableDataInfo pagingNos(String nos, String keyword, int pageNum, int pageSize) {
      List<String> totalNoList = JsonUtils.parseArray(nos, String.class);
      if (StringUtils.hasText(keyword)) {
         totalNoList =totalNoList.stream().filter((s) -> s.contains(keyword)).collect(Collectors.toList());
      }

      int total = totalNoList.size();
      List<String> noList = totalNoList.subList(pageNum * pageSize, Math.min((pageNum + 1) * pageSize, total));
      TableDataInfo rspData = new TableDataInfo();
      rspData.setCode(200);
      rspData.setRows(noList);
      rspData.setMsg("查询成功");
      rspData.setTotal((long)total);
      return rspData;
   }
}
